package com.booking.common.utils;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Qikai.Lin
 * @date 2023/5/10
 */
public class DtUtils {

    private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    public static String getDtStr(LocalDateTime time){
        return LocalDateTimeUtils.convertToStrFromTime(time);
    }

    public static String getYesterdayDtStr(String time){
        LocalDateTime nowTime = LocalDateTimeUtils.convertFromDayStr(time);
        LocalDateTime yesterdayTime = nowTime.minusDays(1);
        return LocalDateTimeUtils.convertToStrFromTime(yesterdayTime);
    }

    public static List<String> getDtRangeFromDtStr(String fromTime, String toTime) {
        LocalDateTime beginTime = LocalDateTimeUtils.convertFromDayStr(fromTime);
        LocalDateTime endTime = LocalDateTimeUtils.convertFromDayStr(toTime);
        return getDtRange(beginTime, endTime);
    }

    public static List<String> getDtRange(LocalDateTime fromTime, LocalDateTime toTime){
        List<String> dtRanges = new ArrayList<>();
        if (fromTime.compareTo(toTime) >= 0 ) {
            dtRanges.add(DtUtils.getDtStr(fromTime));
            return dtRanges;
        }
        while(true){
            int compare = fromTime.compareTo(toTime);
            if(compare > 0){
                break;
            }
            dtRanges.add(DtUtils.getDtStr(fromTime));
            fromTime = fromTime.plusDays(1L);
        }

        return dtRanges;
    }

    public static String dateToDtStr(Date d) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(d);
    }

    public static String dateToStr(Date d) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(d);
    }

    public static void main(String[] args) {
        System.out.println(DtUtils.getDtRange(LocalDateTimeUtils.convertFromDayStr("2022-10-01"),
                LocalDateTimeUtils.convertFromDayStr("2022-10-05")));
    }
}
